﻿
using Baosight.ZNH.ZMCommon;
using System;
using System.Windows;
using System.Windows.Controls;
using Baosight.Platform.Core;
using System.Collections.Generic;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
namespace Baosight.ZNH.ZMCM.UI
{
    /// <summary>
    /// 初始数据从表  
    /// </summary>
    /// Title:ZMCM51
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 李红星
    /// Version: 1.0
    /// History:
    ///       2011-6-24 17:34:21  李红星   [创建]
    ///
    public partial class ZMCM51 : EFViewCmes
    {
        public ZMCM51()
        {
            InitializeComponent();
            this.BeforeAction += OnBeforeAction;
            this.LayoutUpdated += new EventHandler(ZMCM51_LayoutUpdated);
        }

        /// <summary>
        /// 主表生效，明细表按钮失效方法
        /// 此处根据主表字段fdStatus进行判断。主表记录切换的时候将fdStatus传递到从表页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ZMCM51_LayoutUpdated(object sender, EventArgs e)
        {
            String zbStatus = "草拟";
            if (this.fdStatus.Value != null)
                zbStatus = (String)this.fdStatus.Value;
            if (zbStatus.Equals("生效"))
            {
                Button insertbut = this.FindName("INSERT") as Button;
                if (insertbut != null)
                    insertbut.IsEnabled = false;
                Button updatebut = this.FindName("UPDATE") as Button;
                if (updatebut != null)
                    updatebut.IsEnabled = false;
                Button deletebut = this.FindName("DELETE") as Button;
                if (deletebut != null)
                    deletebut.IsEnabled = false;
            }
        }

        /// <summary>
        /// 新增/修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.sub_grid.GetCheckedEIRows();
            _view.Args.Add("fdInventoryId", this.fdInventoryId.Value.ToString());
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 盘点明细新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            //生成
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "盘点明细-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 盘点明细修改操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.sub_grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.sub_grid.GetCheckedEIRows();
            (sender as Button).IsEnabled = false;
            String zb =(String) this.fdStatus.Value;
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "盘点明细-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo eIInfo, string title, string dialogType, string _args)
        {
            eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
            InventoryDetail inventoryDetail = new InventoryDetail(dialogType);//构造一个新的修改页面
            inventoryDetail.EIDataContext = new Platform.Core.UI.EIDataContext();
            inventoryDetail.fdInventoryId.Value = this.fdInventoryId.Value.ToString();
            inventoryDetail.fdDepotId.Value = this.fdDepotId.Value;
            inventoryDetail.EIDataContext.Update(eIInfo);

            if (!this.fdDepotId.Value.Equals("01"))
            {
                inventoryDetail.fdHasInvoiceLoss.Visibility = Visibility.Collapsed;
                inventoryDetail.fdHasInvoiceNum.Visibility = Visibility.Collapsed;
                inventoryDetail.fdNoInvoiceLoss.Visibility = Visibility.Collapsed;
                inventoryDetail.fdNoInvoiceNum.Visibility = Visibility.Collapsed;
                inventoryDetail.fdReason.Width = 675;
            }
            else
            {
                inventoryDetail.fdActualSum.Visibility = Visibility.Collapsed;
                inventoryDetail.fdReason.Width = 685;
            }
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = inventoryDetail;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.sub_grid.Refresh();//刷新Grid区域
            }
            base.TempButtonOpen(true);//按钮生效
        }
    }
}

